package org.projetjava.metier.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.projetjava.metier.model.*;

import org.projetjava.metier.model.HyperText;

public class HyperTextDao extends Bdd{

	//Objet de connexion
	private Connection con;
	
	private PreparedStatement prepStatHyperTextNews = null;
	private PreparedStatement prepStatHyperTextLieu = null;
	private PreparedStatement prepStatHyperTextPoi = null;
	private PreparedStatement prepStatHyperTextParcours = null;
	private PreparedStatement prepStatAjoutHyperTextNews = null;
	private PreparedStatement prepStatAjoutHyperTextParcours = null;
	private PreparedStatement prepStatAjoutHyperTextLieu = null;
	
	//Constructeur de la classe
	public HyperTextDao()
	{
		super();
		this.con = this.getConnection();
	}
	
	public List<HyperText> SelectHyperTextNews(int idNews)
	{
		
		List<HyperText> listHyperText = new ArrayList<>();
		
		
		try {
			
			
			if(this.prepStatHyperTextNews == null)
			{
				this.prepStatHyperTextNews = this.con.prepareStatement("SELECT Lien_HyperText,LibelleHyperTextText FROM LienHyperText WHERE ID_News = ?");
			}
			//list hypertext
			this.prepStatHyperTextNews.setInt(1, idNews);
			ResultSet resultHyperText = this.prepStatHyperTextNews.executeQuery();
			while(resultHyperText.next()){
				HyperText hyperText = new HyperText();
				
				hyperText.getLienHyperText().add(resultHyperText.getObject(1).toString());
				hyperText.getNomLienHyperText().add(resultHyperText.getObject(2).toString());
				
				listHyperText.add(hyperText);
				
			}
			} catch (SQLException e) { 
			e.printStackTrace();
			}
		return listHyperText;
		
	}
	
	public List<HyperText> SelectHyperTextLieu(int idLieu)
	{
		
		List<HyperText> listHyperText = new ArrayList<>();
		
		
		try {
			
			
			if(this.prepStatHyperTextLieu == null)
			{
				this.prepStatHyperTextLieu = this.con.prepareStatement("SELECT Lien_HyperText,LibelleHyperTextText FROM LienHyperText WHERE ID_Lieu = ?");
			}
			//list hypertext
			this.prepStatHyperTextLieu.setInt(1, idLieu);
			ResultSet resultHyperText = this.prepStatHyperTextLieu.executeQuery();
			while(resultHyperText.next()){
				HyperText hyperText = new HyperText();
				
				hyperText.getLienHyperText().add(resultHyperText.getObject(1).toString());
				hyperText.getNomLienHyperText().add(resultHyperText.getObject(2).toString());
				
				listHyperText.add(hyperText);
				
			}
			} catch (SQLException e) { 
			e.printStackTrace();
			}
		return listHyperText;
		
	}
	
	public List<HyperText> SelectHyperTextPoi(int idPoi)
	{
		
		List<HyperText> listHyperText = new ArrayList<>();
		
		
		try {
			
			
			if(this.prepStatHyperTextPoi == null)
			{
				this.prepStatHyperTextPoi = this.con.prepareStatement("SELECT Lien_HyperText,LibelleHyperTextText FROM LienHyperText WHERE ID_Poi = ?");
			}
			//list hypertext
			this.prepStatHyperTextPoi.setInt(1, idPoi);
			ResultSet resultHyperText = this.prepStatHyperTextPoi.executeQuery();
			while(resultHyperText.next()){
				HyperText hyperText = new HyperText();
				
				hyperText.getLienHyperText().add(resultHyperText.getObject(1).toString());
				hyperText.getNomLienHyperText().add(resultHyperText.getObject(2).toString());
				
				listHyperText.add(hyperText);
				
			}
			} catch (SQLException e) { 
			e.printStackTrace();
			}
		return listHyperText;
		
	}
	
	public List<HyperText> SelectHyperTextParcours(int idParcours)
	{
		
		List<HyperText> listHyperText = new ArrayList<>();
		
		
		try {
			
			
			if(this.prepStatHyperTextParcours == null)
			{
				this.prepStatHyperTextParcours = this.con.prepareStatement("SELECT Lien_HyperText,LibelleHyperTextText FROM LienHyperText WHERE ID_Parcours = ?");
			}
			//list hypertext
			this.prepStatHyperTextParcours.setInt(1, idParcours);
			ResultSet resultHyperText = this.prepStatHyperTextParcours.executeQuery();
			while(resultHyperText.next()){
				HyperText hyperText = new HyperText();
				
				hyperText.getLienHyperText().add(resultHyperText.getObject(1).toString());
				hyperText.getNomLienHyperText().add(resultHyperText.getObject(2).toString());
				
				listHyperText.add(hyperText);
				
			}
			} catch (SQLException e) { 
			e.printStackTrace();
			}
		return listHyperText;
		
	}
	
	public int AjoutHyperTextNews(News news,int idNews)
	{

		try {
			if(this.prepStatAjoutHyperTextNews == null)
			{
				this.prepStatAjoutHyperTextNews = this.con.prepareStatement("INSERT INTO LienHyperText(Lien_HyperText, LibelleHyperTextText, ID_News) VALUES (?,?,?)");
			}
			
			for(HyperText hypertext: news.getHypertext())
			{
				for(int i = 0; i < hypertext.getLienHyperText().size() ; i++)
				{
					this.prepStatAjoutHyperTextNews.setString(1, hypertext.getLienHyperText().get(i));
					this.prepStatAjoutHyperTextNews.setString(2, hypertext.getNomLienHyperText().get(i));
					this.prepStatAjoutHyperTextNews.setInt(3, idNews);
					this.prepStatAjoutHyperTextNews.executeUpdate();
				}
			}
			
		} catch (SQLException e) { 
			e.printStackTrace();
			return 1;
	    }
		
		return 0;
	}
	
	public int AjoutHyperTextParcours(Parcours parcours,int idParcours)
	{

		try {
			if(this.prepStatAjoutHyperTextParcours == null)
			{
				this.prepStatAjoutHyperTextParcours = this.con.prepareStatement("INSERT INTO LienHyperText(Lien_HyperText, LibelleHyperTextText, ID_Parcours) VALUES (?,?,?)");
			}
			
			for(HyperText hypertext: parcours.getLienHyperText())
			{
				for(int i = 0; i < hypertext.getLienHyperText().size() ; i++)
				{
					this.prepStatAjoutHyperTextParcours.setString(1, hypertext.getLienHyperText().get(i));
					this.prepStatAjoutHyperTextParcours.setString(2, hypertext.getNomLienHyperText().get(i));
					this.prepStatAjoutHyperTextParcours.setInt(3, idParcours);
					this.prepStatAjoutHyperTextParcours.executeUpdate();
				}
			}
			
		} catch (SQLException e) { 
			e.printStackTrace();
			return 1;
	    }
		
		return 0;
	}
	
	public int AjoutHyperTextLieu(Lieu lieu,int idLieu)
	{

		try {
			if(this.prepStatAjoutHyperTextLieu == null)
			{
				this.prepStatAjoutHyperTextLieu = this.con.prepareStatement("INSERT INTO LienHyperText(Lien_HyperText, LibelleHyperTextText, ID_Lieu) VALUES (?,?,?)");
			}
			
			for(HyperText hypertext: lieu.getLienHyperText())
			{
				for(int i = 0; i < hypertext.getLienHyperText().size() ; i++)
				{
					this.prepStatAjoutHyperTextLieu.setString(1, hypertext.getLienHyperText().get(i));
					this.prepStatAjoutHyperTextLieu.setString(2, hypertext.getNomLienHyperText().get(i));
					this.prepStatAjoutHyperTextLieu.setInt(3, idLieu);
					this.prepStatAjoutHyperTextLieu.executeUpdate();
				}
			}
			
		} catch (SQLException e) { 
			e.printStackTrace();
			return 1;
	    }
		
		return 0;
	}
	
	public int AjoutHyperTextPoi(Poi poi,int idPoi)
	{

		try {
			if(this.prepStatAjoutHyperTextLieu == null)
			{
				this.prepStatAjoutHyperTextLieu = this.con.prepareStatement("INSERT INTO LienHyperText(Lien_HyperText, LibelleHyperTextText, ID_POI) VALUES (?,?,?)");
			}
			
			for(HyperText hypertext: poi.getLienHyperText())
			{
				for(int i = 0; i < hypertext.getLienHyperText().size() ; i++)
				{
					this.prepStatAjoutHyperTextLieu.setString(1, hypertext.getLienHyperText().get(i));
					this.prepStatAjoutHyperTextLieu.setString(2, hypertext.getNomLienHyperText().get(i));
					this.prepStatAjoutHyperTextLieu.setInt(3, idPoi);
					this.prepStatAjoutHyperTextLieu.executeUpdate();
				}
			}
			
		} catch (SQLException e) { 
			e.printStackTrace();
			return 1;
	    }
		
		return 0;
	}
	
	
}
